﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ProjectEulerSolutions
{
    /*
     * The arithmetic sequence, 1487, 4817, 8147, in which each of the terms increases by 3330, is unusual in two ways: (i) each of the three terms are prime, and, (ii) each of the 4-digit numbers are permutations of one another.

There are no arithmetic sequences made up of three 1-, 2-, or 3-digit primes, exhibiting this property, but there is one other 4-digit increasing sequence.

What 12-digit number do you form by concatenating the three terms in this sequence?

     * */
    class Problem49 : IProblem
    {
        public string Calculate()
        {
            //taktika: bruteforce
            //počevši od 1000, gledamo jeli broj prime
            //ako je prime, nađemo sve permutacije
            //  listu permutacija filtriramo na samo veće brojeve i primeove i poredamo
            //  za svaki od brojeva provjerimo jeli postoji slijedeći u ostalim permutacijama
            //     ako postoji, to je rješenje!

            string result = "";

            for (int i = 1000; i < 10000; i++)
            {
                if (CommonFunctions.IsPrime(i))
                {
                    var permutations = CommonFunctions.GetPermutations(CommonFunctions.GetDigits(i));
                    var filteredOrdered = permutations.Where(x => x > i && CommonFunctions.IsPrime(x)).OrderBy(x => x);

                    foreach (int permutation in filteredOrdered)
                    {
                        if (filteredOrdered.Contains(2 * permutation - i))
                        {
                            result = i + "" + permutation + "" + (2 * permutation - i);
                            Console.WriteLine(result);
                        }
                    }
                }
            }

            return result;
            //throw new NotImplementedException();
        }
    }
}
